package com.wyvoid.forum.web.action.user;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;
import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.web.tag.ForumFunctions;
import com.wyvoid.forum.Constant;
import com.wyvoid.forum.domain.SessionUser;
import com.wyvoid.core.util.CookieUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-14
 * Time: 21:58:43
 */
@UrlBinding("/user/logout.do")
public class LogoutAction extends BaseAction {
    @DefaultHandler
    public Resolution logout() throws Exception {
        getContext().logout();
        CookieUtils.removeCookie(context.getResponse(), Constant.cookieName, null);
        String targetUrl = context.getRequest().getParameter("referer");
        if (targetUrl == null) {
            targetUrl = context.getRequest().getHeader("referer");
        }
        if (StringUtils.isNotBlank(targetUrl) && targetUrl.indexOf("leftMenu.do") == -1) {
            return new RedirectResolution(targetUrl, false);
        } else {
            return new RedirectResolution("/");
        }
    }

    public Resolution switchStatus() {
        HttpServletResponse response=context.getResponse();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        SessionUser sessionUser = context.getUser();
        if (sessionUser != null) {
            ForumFunctions.switchOnlineStatus(sessionUser.getId());
        }

        String targetUrl = context.getRequest().getParameter("referer");
        if (targetUrl == null) {
            targetUrl = context.getRequest().getHeader("referer");
        }
        if (StringUtils.isNotBlank(targetUrl) && targetUrl.indexOf("leftMenu.do") == -1) {
            return new RedirectResolution(targetUrl, false).addParameter("ra",Math.random());
        } else {
            return new RedirectResolution("/").addParameter("ra",Math.random());
        }
    }
}
